package com.tutor.lzm.function.authenticate.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.fragment.BaseFragment;
import com.tutor.lzm.function.authenticate.fragment.EducationCertificationFragment;
import com.tutor.lzm.function.authenticate.fragment.TeacherQualificationFragment;
import com.tutor.lzm.function.authenticate.fragment.VerifiedFragment;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.TeacherAuthenticationRequest;
import com.tutor.lzm.utils.ImageUtils;
import com.tutor.lzm.utils.SPKey;
import com.tutor.lzm.utils.SharedPreferenceHelper;
import com.tutor.syslib.util.JsonParse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zzgpc on 2017-9-17.
 * 认证成为老师
 */
public class CertificationTeacherAct extends BaseActivity {


    @BindView(R.id.tv_audit_one)
    TextView tvAuditOne;
    @BindView(R.id.tv_audit_two)
    TextView tvAuditTwo;
    @BindView(R.id.tv_audit_three)
    TextView tvAuditThree;

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, CertificationTeacherAct.class);
        intent.putExtra("type", type);
        context.startActivity(intent);

    }

    /**
     * 认证请求实体
     */
    private TeacherAuthenticationRequest request;

    public TeacherAuthenticationRequest getRequest() {
        if (request == null) {
            request = AppSession.getSPDataObject(SPKey.teacherAuthentication, TeacherAuthenticationRequest.class);
        }
        if (request == null) {
            request = new TeacherAuthenticationRequest();
        }
        return request;
    }

    public void saveAuthenticationRequest(TeacherAuthenticationRequest request) {

        AppSession.saveAppSessionData(SPKey.teacherAuthentication, request);

    }

    public void setRequest(TeacherAuthenticationRequest request) {
        this.request = request;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_certifacation_teacher);
        ButterKnife.bind(this);
        request = getRequest();
        initView();

    }


    private void initView() {
        initTitle("认证成为老师");
        String type = getIntent().getStringExtra("type");
        update(type);
    }

    public void update(String status) {
        request = getRequest();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", request);
        if (status.equals("1")) {
            VerifiedFragment verifiedFragment = new VerifiedFragment();
            replaceFragment(verifiedFragment, bundle);
        } else if (status.equals("2")) {
            tvAuditTwo.setBackground(getResources().getDrawable(R.drawable.blue_round));
            EducationCertificationFragment educationCertificationFragment = new EducationCertificationFragment();
            replaceFragment(educationCertificationFragment, bundle);
        } else if (status.equals("3")) {
            tvAuditThree.setBackground(getResources().getDrawable(R.drawable.blue_round));
            TeacherQualificationFragment teacherQualificationFragment = new TeacherQualificationFragment();
            replaceFragment(teacherQualificationFragment, bundle);
        }
    }

    /**
     * 提交
     */
    public void upload() {
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                //提交成功，等待审核

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                //提交成功，等待审核
            }
        });

    }

    /**
     * 在fragment的管理类中，我们要实现这部操作，而他的主要作用是，当D这个activity回传数据到
     * 这里碎片管理器下面的fragnment中时，往往会经过这个管理器中的onActivityResult的方法。
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*在这里，我们通过碎片管理器中的Tag，就是每个碎片的名称，来获取对应的fragment*/
        int size = notifyBackPressList.size();
        if (size > 0) {
            BaseFragment f = notifyBackPressList.get(size - 1);
        /*然后在碎片中调用重写的onActivityResult方法*/
            f.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onBackPressed() {
//        if(mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()){
        if (notifyBackPressList != null && notifyBackPressList.size() > 0) {
            notifyBackPressList.clear();
            super.onBackPressed();
//            getSupportFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
}
